package org.testis.testdriver.validation;

import org.testis.testdriver.AtofEnumaration;
import org.testis.testdriver.TestIterator;
import org.testis.testdriver.TestPart;
import org.testis.testdriver.exceptions.NoSuchValidateStrategyException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 *
 * @author barisergun
 */
public class TestPartValidateContext {

    private TestPartValidateStrategy testPartRunSrategy = null;

    public TestPartValidateContext(TestPart testPartToRun) {

        try {

            Class<?> cls = Class.forName("org.testis.testdriver.validation.TestPartValidate"
                    + testPartToRun.getContainerType().
                    toString() + "Strategy");
            Constructor[] constr = cls.getDeclaredConstructors();
            Object[] params = new Object[]{testPartToRun};
            for (int i = 0; i < constr.length; i++) {
                Class[] constParams = constr[i].getParameterTypes();
                if (constParams.length == 1) {
                    testPartRunSrategy = (TestPartValidateStrategy) constr[i].newInstance(
                            params);

                }
            }

        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (IllegalArgumentException ex) {
        } catch (ClassNotFoundException ex) {
        } catch (InvocationTargetException ex) {
        } finally {
            if (testPartRunSrategy == null) {
                throw new NoSuchValidateStrategyException(
                        "TestPartValidate"
                        + testPartToRun.getContainerType().
                        toString() + "Strategy does not exist");
            }
        }



    }

    public boolean validatePartResult(TestIterator testIterInRun,
            AtofEnumaration.DebuggingMode mode,
            boolean partResult) {
        return testPartRunSrategy.validatePartResult(testIterInRun,
                mode,
                partResult);
    }

    public boolean validateFinalPartResult(TestIterator testIterInRun,
            AtofEnumaration.DebuggingMode mode,
            boolean partResult) {
        return testPartRunSrategy.validateFinalPartResult(testIterInRun,
                mode,
                partResult);
    }
}
